﻿using System.Collections.Generic;
using MyUtil;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
using UnityEngine.UIElements;

namespace KnightWeapons
{
    public class Gun : BaseWeapons<GunData>
    {        
        [SerializeField] private GameObject bulletPrefab;
        [SerializeField] Transform bulletLaunchTF;

        public override void OnNetworkSpawn()        
        {
            if (!IsOwner)
                return;
            
            base.Init();            
            knight = NetworkManager.LocalClient.PlayerObject.GetComponent<Knight>();            
            attackCdTimer = TimerManager.Instance.CreateTimer(weaponData.AttackCD, OnAttackCdTimeout, true);
        }

        public override void OnNetworkDespawn()
        {
            if (!IsOwner)
                return;
            TimerManager.Instance.RemoveTimer(attackCdTimer);
        }

        private void Update()
        {            
            if (!IsOwner)
                return;

            if (currentAimTransform == null || !currentAimTransform.gameObject.activeInHierarchy)
            {                
                FindEnemy(weaponData.AimDistance);
            }
            else
            {
                float dis = (currentAimTransform.position-transform.position).sqrMagnitude;
                float sqrAimDis = weaponData.AimDistance * weaponData.AimDistance;
                if (dis > sqrAimDis)
                    FindEnemy(weaponData.AimDistance);
                else if (dis > sqrAimDis)
                    FindEnemy(weaponData.AttackDistance);
            }

            if (currentAimTransform != null  && !isAttack)
                OnAim();
        }

        void OnAttackCdTimeout()
        {
            canAttack = true;
        }

        protected override void OnAttack()
        {
            canAttack =false;
            attackCdTimer.Start();
            
            networkAnimator.SetTrigger("Fire");            
        }
        
        //动画结束时通过AnimationEvent调用OnFire;
        void OnFire()
        {
            if(!IsOwner) return;
            var predictBullet = Instantiate(bulletPrefab,bulletLaunchTF.position,transform.rotation).GetComponent<Bullet>();
            predictBullet.Fire();
            WeaponManager.Instance.PredictIdx++;
            int predictIdx = WeaponManager.Instance.PredictIdx;
            WeaponManager.Instance.PredictBullets[predictIdx]= predictBullet;
            ShootRpc(OwnerClientId,bulletLaunchTF.position,transform.rotation,predictIdx);
        }

        [Rpc(SendTo.Server)]
        void ShootRpc(ulong ownerId,Vector2 pos,Quaternion rotation,int predictIdx)
        {  
            var bullet = Instantiate(bulletPrefab, pos,rotation).GetComponent<Bullet>();
            bullet.PredictBulletIdx.Value = predictIdx;
            bullet.NetworkObject.SpawnWithOwnership(ownerId, true);            
        }

        protected override void OnResume()
        {
            
        }
    }
}
